class KuaiPanError(Exception):
    default_message = u"Unspecified KuaiPan Error"

    def __init__(self, details=u'', msg=None):
        if msg is None:
            msg = self.default_message
        self.msg = msg
        self.details = details

    def __str__(self):
        import sys
        encoding = sys.getfilesystemencoding()
        return '%s, detail = %s' % (self.msg.encode(encoding), self.details.encode(encoding))

    def __unicode__(self):
        return unicode(u'%s, detail = %s' % self.msg, self.details)


class KuaiPanNetError(KuaiPanError):
    default_message = u"Network Error"

class KuaiPanDataError(KuaiPanError):
    defualt_message = u"Data Error"

class KuaiPanNeedToImplement(KuaiPanError):
    defualt_message = u"Need To Implement"


class KuaiPanError202(KuaiPanError):  
    default_message = u"KuaiPan Run Error"

class KuaiPanError400(KuaiPanError):  
    default_message = u"KuaiPan Parameter Error"

class KuaiPanError401(KuaiPanError):  
    default_message = u"KuaiPan Authorization Error"

class KuaiPanError403(KuaiPanError):  
    default_message = u"KuaiPan No Permission"

class KuaiPanError404(KuaiPanError):  
    default_message = u"KuaiPan Not Exist"
    
class KuaiPanError405(KuaiPanError):  
    default_message = u"KuaiPan Path Wrong"

class KuaiPanError406(KuaiPanError):  
    default_message = u"KuaiPan Too Many Files"

class KuaiPanError413(KuaiPanError):  
    default_message = u"KuaiPan Too Large File"

class KuaiPanError500(KuaiPanError):  
    default_message = u"KuaiPan Server Internal Error"

class KuaiPanError507(KuaiPanError):  
    default_message = u"KuaiPan Space Not Enough"
    
class KuaiPanError5xx(KuaiPanError):  
    default_message = u"KuaiPan Server Error, Out of Service"
    
def check_status(status, data=''):
    ex_map = {202: KuaiPanError202,
              400: KuaiPanError400,
              401: KuaiPanError401,
              403: KuaiPanError403,
              404: KuaiPanError404,
              405: KuaiPanError405,
              406: KuaiPanError406,
              413: KuaiPanError413,
              500: KuaiPanError500,
              507: KuaiPanError507,
              }
    if 200 == status:
        return
    ex = ex_map.get(status)
    if ex is not None:
        raise ex, data
    elif 500 < status and status < 600:
        raise KuaiPanError5xx, data
    else:
        raise KuaiPanError, data, u'KuaiPan Unknown Error'

